package com.shalanzi.hospital.controller;

import com.shalanzi.hospital.base.BasePath;
import com.shalanzi.hospital.pojo.User;
import com.shalanzi.hospital.query.UserQuery;
import com.shalanzi.hospital.service.UserService;
import com.shalanzi.hospital.vo.ResultInfo;
import com.shalanzi.hospital.vo.UserModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Dream_飞翔
 * @date 2021/9/24
 * @time 21:12
 * @email 1072876976@qq.com
 */
@Controller
@RequestMapping("/user")
public class UserController extends BasePath {
    @Resource
    private UserService userService;

    @RequestMapping("/index")
    public String index() {
        return "user/index";
    }

    // 修改密码
    @RequestMapping("/adminInfo")
    public String adminInfo() {
        return "user/admin-info";
    }

    //个人资料
    @RequestMapping("/oneSet")
    public String oneSet() {
        return "user/oneSet";
    }

    @RequestMapping("/toUserPage")
    public String toUserPage(Integer userId, HttpServletRequest request) {
        // 判断用户Id是否为空
        if (userId != null) {
            // 通过用户Id查询用户对象
            User user = userService.selectUserById(userId);
            // 将查询到的User对象设置到请求域中
            request.setAttribute("updateUser", user);
        }
        return "user/addOrUpdate";
    }

    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> getUserList(UserQuery userQuery) {
        return userService.getUserList(userQuery);
    }

    @PostMapping("/login")
    @ResponseBody
    public ResultInfo userLogin(String userName, String password) {
        UserModel userModel = userService.userLogin(userName, password);
        return userModel == null ? ResultInfo.fail() : ResultInfo.success(userModel);
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateUserById(User user) {
        Integer row = userService.updateUserById(user);
        return row > 0 ? ResultInfo.success("用户数据更新成功！") : ResultInfo.fail("用户数据更新失败！");
    }

    @PostMapping("/add")
    @ResponseBody
    public ResultInfo addUserById(User user) {
        Integer row = userService.addUserById(user);
        return row > 0 ? ResultInfo.success("新用户添加成功！") : ResultInfo.fail("用户添加失败！");
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public ResultInfo deleteUserById(Integer[] ids) {
        Integer rows = userService.deleteUserByIds(ids);
        return rows != ids.length ? ResultInfo.fail("用户信息删除失败！") : ResultInfo.success("用户信息删除成功！");
    }

    @GetMapping("/queryAllGroups")
    @ResponseBody
    public List<Map<String, Object>> queryAllGroups() {
        return userService.queryAllGroups();
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public ResultInfo updatePassword(Integer userId, String newPassword, String rePassword) {
        Integer row = userService.updatePassword(userId, newPassword, rePassword);
        return row > 0 ? ResultInfo.success() : ResultInfo.fail("用户密码修改失败！");
    }

}
